package com.aw.guiguzi.net.observers;

import com.aw.baselib.util.AwDateUtil;
import com.aw.guiguzi.bean.UserInfoBean;
import com.aw.guiguzi.constants.MyConstant;
import com.aw.guiguzi.net.NetClient;
import com.aw.guiguzi.net.request.NetRequest;
import com.aw.guiguzi.util.AppUserUtil;

import java.io.File;
import java.util.Calendar;
import java.util.HashMap;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class NetObCommon {

    public static Observable login(String account, String pwd) {
        return NetClient.getCommonService().login(NetRequest.login(account, pwd));
    }

    public static Observable createUser(UserInfoBean bean) {
        return NetClient.getCommonService().createUser(NetRequest.createUser(bean));
    }

    public static Observable userRoleAssign(String id) {
        return NetClient.getCommonService().userRoleAssign(NetRequest.userRoleAssign(id));
    }

    public static Observable getUserInfo() {
        return NetClient.getCommonService().getUserInfo();
    }

    public static Observable getUserDetail(String id) {
        return NetClient.getCommonService().getUserDetail(id);
    }

    public static Observable updateUserInfo(UserInfoBean bean) {
        return NetClient.getCommonService().updateUserInfo(NetRequest.updateUserInfo(bean));
    }

    public static Observable updatePwd(String pwd) {
        return NetClient.getCommonService().updatePwd(NetRequest.updatePwd(pwd));
    }

    public static Observable updateStatus(String id, boolean isEnable) {
        return NetClient.getCommonService().updateStatus(NetRequest.updateStatus(id, isEnable));
    }

    public static Observable getAllDeviceToken() {
        return NetClient.getCommonService().getAllDeviceToken(1, 100, AppUserUtil.getUserId());
    }

    public static Observable delToken(String token) {
        return NetClient.getCommonService().delToken(token);
    }

    public static Observable delUser(String id) {
        return NetClient.getCommonService().delUser(id);
    }

    public static Observable uploadFile(String filePath) {
        File file = new File(filePath);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
        //        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", MimeType.fileToString(filePath));
        return NetClient.getCommonService().upload(filePart);
    }

    public static Observable getUserList(int pageNo) {
        return NetClient.getCommonService().getUserList(pageNo, MyConstant.COMMON_LIST_PAGE_LIMIT_LARGE);
    }

    public static Observable getUserListSimple() {
        return NetClient.getCommonService().getUserListSimple();
    }

    public static Observable resetPwd(String id, String pwd) {
        return NetClient.getCommonService().resetPwd(NetRequest.resetPwd(id, pwd));
    }

    public static Observable getFileList(int pageNo) {
        return NetClient.getCommonService().getFileList(pageNo, MyConstant.COMMON_LIST_PAGE_LIMIT_LARGE);
    }

    public static Observable delFile(String id) {
        return NetClient.getCommonService().delFile(id);
    }

    public static Observable getLoginRecordList(int pageNo, String username, boolean isLimit7Days) {
        if(isLimit7Days) {
            //默认限定7天时间范围, 数据太多了.
            String pre7Days = AwDateUtil.getFormatSpecDate(System.currentTimeMillis(), Calendar.DAY_OF_YEAR, -7, AwDateUtil.FORMAT_YMD_HMS);
            String currentTime = AwDateUtil.formatUnixTime(System.currentTimeMillis());
            String[] arrays = {pre7Days, currentTime};
            return NetClient.getCommonService().getLoginRecordList(pageNo, MyConstant.COMMON_LIST_PAGE_LIMIT_LARGE, username, arrays);
        } else {
            return NetClient.getCommonService().getLoginRecordList(pageNo, MyConstant.COMMON_LIST_PAGE_LIMIT_LARGE, username);
        }
    }

    public static Observable getMsgList(int pageNo, boolean filterUnRead) {
        if(filterUnRead) {
            return NetClient.getCommonService().getMsgList(pageNo, MyConstant.COMMON_LIST_PAGE_LIMIT_LARGE, false);
        } else {
            return NetClient.getCommonService().getMsgList(pageNo, MyConstant.COMMON_LIST_PAGE_LIMIT_LARGE);
        }

    }

    public static Observable msgReadAll() {
        return NetClient.getCommonService().msgReadAll();
    }

    public static Observable msgRead(String id) {
        String[] ids = new String[1];
        ids[0] = id;
        return NetClient.getCommonService().msgRead(ids);
    }

    public static Observable getMsgListManager(int pageNo) {
        return NetClient.getCommonService().getMsgListManager(pageNo, MyConstant.COMMON_LIST_PAGE_LIMIT_LARGE);
    }

    public static Observable getMsgUnreadCount() {
        return NetClient.getCommonService().getMsgUnreadCount();
    }

    public static Observable msgCreateToSend(String id, String templateId, HashMap<String, String> content) {
        return NetClient.getCommonService().msgCreateToSend(NetRequest.msgCreateToSend(id, templateId, content));
    }

    public static Observable getMsgTemplateList(int pageNo) {
        return NetClient.getCommonService().getMsgTemplateList(pageNo, MyConstant.COMMON_LIST_PAGE_LIMIT_LARGE);
    }
}
